﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityFigmaBridge.Editor.Utils;

namespace UnityFigmaBridge.Editor.PrototypeFlow
{
    /// <summary>
    /// Generates CS file with flowScreen names as constants for reference by functionality
    /// </summary>
    public static class ScreenNameCodeGenerator
    {
        public static void WriteScreenNamesCodeFile(List<GameObject> screenObjects)
        {
            
            var outputPath = $"{FigmaPaths.FigmaAssetsRootFolder}/ScreenNames.cs";

            var content = @"
using System;
 
public class FigmaScreenNames {

    // Automatically generated by UnityFigmaBridge
          
";
            foreach (var screen in screenObjects)
            {
                var screenName = screen.name;
                content += $"\tpublic const string {GetCodeSafeNameForScreen(screenName)}=\"{screenName}\";\n";
            }
            
            content+=@"
}
";
                
                
            File.WriteAllText(outputPath, content);
            // Reimport
            AssetDatabase.ImportAsset(outputPath);
        }

        public static string GetCodeSafeNameForScreen(string screenName)
        {
            var safeTitle = FigmaPaths.MakeValidFileName(screenName.ToUpper());
            safeTitle = safeTitle.Replace(" ", "_");
            return safeTitle;
        }
    }
}